package com.leetcode.weekly.contest.no286th;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author 覃国强
 * @date 2022-03-27 10:31
 */
public class A01_找出两数组的不同 {



  static
  class Solution {

    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
      Set<Integer> set1 = new HashSet<>();
      if (nums1 != null) {
        Arrays.stream(nums1).forEach(set1::add);
      }

      Set<Integer> set2 = new HashSet<>();
      if (nums2 != null) {
        Arrays.stream(nums2).forEach(set2::add);
      }

      List<Integer> list1 = new ArrayList<>();
      List<Integer> list2 = new ArrayList<>();

      for (Integer num : set1) {
        if (!set2.contains(num)) {
          list1.add(num);
        }
      }

      for (Integer num : set2) {
        if (!set1.contains(num)) {
          list2.add(num);
        }
      }

      List<List<Integer>> answer = new ArrayList<>();

      answer.add(list1);
      answer.add(list2);

      return answer;
    }


  }

  static
  class Solution2 {
    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
      ArrayList<List<Integer>> answer = new ArrayList<>();
      if ((nums1 == null || nums1.length == 0) && (nums2 == null || nums2.length == 0)) {
        return answer;
      }

      if ((nums1 == null || nums1.length == 0) || (nums2 == null || nums2.length == 0)) {
        Set<Integer> set = new HashSet<>();
        int[] nums = (nums1 == null || nums1.length == 0) ? nums2 : nums1;

        for (int num : nums) {
          set.add(num);
        }

        List<Integer> list = new ArrayList<>(set);
        if (nums1 == null || nums1.length == 0) {
          answer.add(new ArrayList<>());
          answer.add(list);
        } else {
          answer.add(list);
          answer.add(new ArrayList<>());
        }
        return answer;
      }

      Set<Integer> set1 = new TreeSet<>();
      Arrays.stream(nums1).forEach(set1::add);
      Integer[] tmp1 = set1.toArray(new Integer[0]);

      Set<Integer> set2 = new TreeSet<>();
      Arrays.stream(nums2).forEach(set2::add);
      Integer[] tmp2 = set2.toArray(new Integer[0]);

      List<Integer> list1 = new ArrayList<>();
      List<Integer> list2 = new ArrayList<>();


      int index1 = 0;
      int index2 = 0;
      while (index1 < tmp1.length && index2 < tmp2.length) {
        if (tmp1[index1].equals(tmp2[index2])) {
          ++index1;
          ++index2;
        } else if (tmp1[index1] < tmp2[index2]) {
          list1.add(tmp1[index1]);
          ++index1;
        } else {
          list2.add(tmp2[index2]);
          ++index2;
        }
      }

      while (index1 < tmp1.length) {
        list1.add(tmp1[index1++]);
      }
      while (index2 < tmp2.length) {
        list2.add(tmp2[index2++]);
      }

      answer.add(list1);
      answer.add(list2);

      return answer;
    }
  }

}
